package com.itwxz.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwxz.common.core.domain.entity.SysUser;
import com.itwxz.common.utils.SecurityUtils;
import com.itwxz.common.utils.bean.BeanUtils;
import com.itwxz.system.domain.po.WxzConfig;
import com.itwxz.system.domain.po.WxzOrder;
import com.itwxz.system.domain.vo.WxzConfigVo;
import com.itwxz.system.enums.order.OrderStatus;
import com.itwxz.system.mapper.WxzConfigMapper;
import com.itwxz.system.service.IWxzConfigService;
import com.itwxz.system.service.IWxzOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 手机端参数配置Service业务层处理
 *
 * @author gastronome
 * @date 2025-04-09
 */
@Service
@RequiredArgsConstructor
public class WxzConfigServiceImpl extends ServiceImpl<WxzConfigMapper, WxzConfig> implements IWxzConfigService {
	private final WxzConfigMapper wxzConfigMapper;

	private final IWxzOrderService orderService;

	/**
	 * 查询手机端参数配置列表
	 *
	 * @param wxzConfig 手机端参数配置
	 * @return 手机端参数配置
	 */
	@Override
	public List<WxzConfig> selectWxzConfigList(WxzConfig wxzConfig) {
		return wxzConfigMapper.selectWxzConfigList(wxzConfig);
	}

	@Override
	public Map<String, Object> getStaffConfig() {
		Map<String, Object> result = lambdaQuery()
				.likeRight(WxzConfig::getConfigKey, "staff")
				.list()
				.stream()
				.map(config -> BeanUtils.copyBean(config, WxzConfigVo.class))
				.collect(Collectors.toMap(WxzConfigVo::getConfigKey, WxzConfigVo::getConfigValue));

		SysUser user = SecurityUtils.getLoginUser().getUser();
		result.put("staff", user);

		Long userId = user.getUserId();

		// ================= 本学期订单总数 =================
		int semesterOrderCount = (int) orderService.count(
				new LambdaQueryWrapper<WxzOrder>()
						.eq(WxzOrder::getStaffId, userId)
						.eq(WxzOrder::getStatus, OrderStatus.COMPLETED.getCode())
		);
		result.put("semesterOrderCount", semesterOrderCount);

		// ================= 本周订单总数 =================
		LocalDateTime startOfWeek = LocalDate.now().with(DayOfWeek.MONDAY).atStartOfDay();
		LocalDateTime endOfWeek = startOfWeek.plusDays(7);

		int weeklyOrderCount = (int) orderService.count(
				new LambdaQueryWrapper<WxzOrder>()
						.eq(WxzOrder::getStaffId, userId)
						.eq(WxzOrder::getStatus, OrderStatus.COMPLETED.getCode())
						.ge(WxzOrder::getCompletionTime, startOfWeek)
						.lt(WxzOrder::getCompletionTime, endOfWeek)
		);
		result.put("weeklyOrderCount", weeklyOrderCount);

		// ================= 进行中订单数 =================
		int inProgressCount = (int) orderService.count(
				new LambdaQueryWrapper<WxzOrder>()
						.eq(WxzOrder::getStaffId, userId)
						.in(WxzOrder::getStatus,
								OrderStatus.IN_PROGRESS.getCode(),
								OrderStatus.ACCEPT.getCode())
		);
		result.put("countInProgress", inProgressCount);

		return result;
	}
}
